package com.j283_5.wisdom_park.controller.tenant_property;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.common.utils.FormatTool;
import com.j283_5.wisdom_park.config.AlipayConfig;
import com.j283_5.wisdom_park.pojo.PropertyPayCost;
import com.j283_5.wisdom_park.service.PropertyPayCostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 蒋翔
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/payCost")
@Api(value = "租户物业费催缴",tags = "租户物业费催缴",produces="蒋翔")
@Transactional
public class TenantPropertyFeeCollection {
    @Resource
    private PropertyPayCostService propertyPayCostService;

    @RequestMapping(value = "/tenantPropertyList" , method = RequestMethod.GET)
    @ApiOperation(value = "查询所有租户物业费催缴",notes = "查询所有租户物业费催缴",produces="蒋翔", response = ResultData.class)
    public ResultData tenantPropertyList(@Param("page")Page<PropertyPayCost> page){
        return propertyPayCostService.tenantPropertyList(page);
    }

    @RequestMapping(value = "/WorthOf" , method = RequestMethod.POST)
    @ApiOperation(value = "物业费催缴",notes = "物业费催缴",produces="蒋翔", response = ResultData.class)
    public ResultData WorthOf(@RequestBody ArrayList<Integer> ppcIdList){
        return propertyPayCostService.WorthOf(ppcIdList);
    }

    @RequestMapping(value = "/peopleTenant" , method = RequestMethod.POST)
    @ApiOperation(value = "查询当前用户催缴账单信息",notes = "查询当前用户催缴账单信息",produces="蒋翔", response = ResultData.class)
    public ResultData peopleTenant(Integer logId){
        return propertyPayCostService.peopleTenant(logId);
    }

    @RequestMapping(value = "/payMoney" , method = RequestMethod.POST)
    @ApiOperation(value = "查询当前用户催缴账单信息",notes = "查询当前用户催缴账单信息",produces="蒋翔", response = ResultData.class)
    public ResultData payMoney(Integer logId){
        return propertyPayCostService.peopleTenant(logId);
    }


    @RequestMapping(value = "/pay",method = RequestMethod.POST)
    @ApiOperation(value = "用户缴费",notes = "用户缴费",produces="蒋翔", response = ResultData.class)
    public ResultData pay(HttpServletRequest request, HttpServletResponse response ,Integer logId, Integer ppc_type ,Double ppc_Money) throws ServletException, IOException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
/////////////////////////////////////////应用程序所需要的 数据///////////////////////////////////////////////
        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = request.getParameter("WIDout_trade_no");
        //付款金额，必填
        String total_amount = ppc_Money.toString();//request.getParameter("WIDtotal_amount");
        //订单名称，必填
        String subject =request.getParameter("WIDsubject");
        //商品描述，可空
        String body = request.getParameter("WIDbody");
        //service   生成订单

//////////////////////////////////////////////////////////////////////////////////

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
            if(result.contains("menu.html")) {
               return propertyPayCostService.pay(logId,ppc_type);
            }
//            response.setContentType("text/html;charset=" + AlipayConfig.charset);
//            response.getWriter().write(result);//直接将完整的表单html输出到页面
//            response.getWriter().flush();
//            response.getWriter().close();
        } catch (AlipayApiException e) {
//            e.printStackTrace();
//            response.getWriter().write("捕获异常出错");
//            response.getWriter().flush();
//            response.getWriter().close();
            return ResultData.error().message("支付失败");
        }
        return ResultData.error().message("网络异常");
    }
}
